package com.michale.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.michale.admin.controller.tool.BaseController;
import com.michale.common.annotation.Log;
import com.michale.common.domain.system.NoticeEntity;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.result.PageResult;
import com.michale.system.service.NoticeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Michale
 * @apiNote 通知管理
 */
@Api(tags = "通知管理")
@RestController
@RequestMapping("system/notice")
public class NoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 分页查询-通知列表
     *
     * @param noticeEntity 查询参数
     * @return 通知列表
     */
    @GetMapping("/noticeList")
    @PreAuthorize("@security.isPermission('system:notice:list')")
    public PageResult getNoticeList(NoticeEntity noticeEntity) {
        startPage();
        List<NoticeEntity> noticeList = noticeService.selectNoticeList(noticeEntity);
        PageInfo<NoticeEntity> pageInfo = new PageInfo<>(noticeList);
        return pageResult(pageInfo);
    }

    /**
     * 根据通知公告编号获取详细信息
     *
     * @param noticeId 公告编号
     * @return 公告信息
     */
    @GetMapping("/{noticeId}")
    @PreAuthorize("@security.isPermission('system:notice:list')")
    public NoticeEntity getNoticeInfo(@PathVariable Long noticeId) {
        return noticeService.selectNoticeById(noticeId);
    }

    /**
     * 新增通知公告
     *
     * @param noticeEntity 公告信息
     * @return 结果
     */
    @PostMapping("/insertNotice")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@security.isPermission('system:notice:add')")
    public AjaxResult insertNotice(@Validated @RequestBody NoticeEntity noticeEntity) {
        return toAjax(noticeService.insertNotice(noticeEntity));
    }

    /**
     * 修改通知公告
     *
     * @param noticeEntity 公告信息
     * @return 结果
     */
    @PutMapping("/updateNotice")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('system:notice:edit')")
    public AjaxResult updateNotice(@Validated @RequestBody NoticeEntity noticeEntity) {
        return toAjax(noticeService.updateNotice(noticeEntity));
    }

    /**
     * 修改公告状态
     *
     * @param noticeEntity 修改参数
     * @return 结果
     */
    @PutMapping("/changeStatus")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('system:user:edit')")
    public AjaxResult changeStatus(NoticeEntity noticeEntity) {
        return toAjax(noticeService.changeStatus(noticeEntity));
    }

    /**
     * 根据公告ID删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @DeleteMapping("/deleteNotice/{noticeIds}")
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@security.isPermission('system:notice:remove')")
    public AjaxResult deleteNotice(@PathVariable Long[] noticeIds) {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }
}
